package top.yohann.study.common.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import top.yohann.study.common.common.Result;

/**
 * @author Yeung Wah
 * @since 2021/11/28
 */
@ApiIgnore
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("get")
    public String get(@RequestParam(defaultValue = "common:message") String key, @RequestParam(defaultValue = "test") String hKey) {
        BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(key);
        return hashOps.get(hKey);
    }

    @GetMapping("set")
    public Result<String> get(@RequestParam(defaultValue = "common:message") String key, @RequestParam(defaultValue = "test") String hKey, String value) {
        BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(key);
        hashOps.put(hKey, value);
        return Result.success();
    }

}